<template>
  <div class="app-container">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select v-model="formInline.status" style="width:100px">
            <el-option v-for="item in status_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="请选择收货门店" v-model="store_id" style="width:300px">
            <el-option v-for="item in store_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="type" style="width:100px">
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input style="width:300px" v-model="formInline.order_id" placeholder="请输入完整采购单号或单号末尾数字" @keyup.enter.native="searchClick"></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购单号" prop="order_id"></el-table-column>
        <el-table-column align="center" label="下单时间" prop="create_time"></el-table-column>
        <el-table-column align="center" label="下单人" prop="order_user" width="100"></el-table-column>
        <el-table-column align="center" label="采购渠道" prop="type" width="100"></el-table-column>
        <el-table-column align="center" label="下单类型" prop="order_type" width="100"></el-table-column>
        <el-table-column align="center" label="收货门店" prop="receiver_store"></el-table-column>
        <el-table-column align="center" label="下单状态" prop="link_people" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.link_people=='店长审批中'">待处理</span>
            <span v-else :class="{danger:scope.row.link_people=='被打回'}">{{scope.row.link_people}}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="采购确认" width="100">
          <template slot-scope="scope">
            <span>-</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="物流发货" width="100">
          <template slot-scope="scope">
            <span>-</span>
          </template>
        </el-table-column> -->
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <router-link v-if="scope.row.link_people=='店长审批中'"
              :to="{name:'warehouse/poInfo',params :{id:scope.row.id,status:1,type:scope.row.type,routerflag:1}}">
              <el-button type="primary" plain size="mini">审批</el-button>
            </router-link>
            <router-link v-else :to="{name:'warehouse/poInfo',params :{id:scope.row.id,status:scope.row.order_type,type:scope.row.type,routerflag:2}}">
              <el-button type="primary" plain size="mini">详情</el-button>
            </router-link>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber"></el-pagination>
    </div>
  </div>
</template>
<script>
import { getStore, getList, getPage } from '@/api/warehouse/storeManagerCheck';
export default {
  data() {
    return {
      listLoading: false,
      all_store_id: '',
      status_list: [
        { id: '1', name: '待处理' },
        { id: '2', name: '已处理' },
        { id: '3', name: '总部统配' }
      ],
      store_list: [],
      type_list: [
        { id: '0', name: '采购渠道' },
        { id: '1', name: '供应商' },
        { id: '2', name: '中央仓' }
      ],
      store_id: '',
      type: '0',
      formInline: {
        status: '1',
        store_id: '',
        order_id: '',
        type: '0'
      },
      list: null,
      totalDataNumber: 1,
      currentPage: 1,
      saveForm: {
        order_id: '',
        order_list: []
      }
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.list) {
        vm.getStoreList();
      }
      if (vm.$route.params.routerflag == 1) {
        vm.handleCurrentChange(vm.currentPage);
      }
    });
  },
  methods: {
    getStoreList() {
      this.listLoading = true;
      getStore({ look_del: 0, category_id: 445 }).then(res => {
        res.forEach(val => {
          this.all_store_id += val.id + ',';
        });
        this.all_store_id = this.all_store_id.substr(0, this.all_store_id.length - 1);
        if (res.length > 1) {
          res.unshift({ id: -1, name: '收货门店' });
        }
        this.store_list = res;
        if (this.store_list.length > 0) {
          this.store_id = this.store_list[0].id;
        } else {
          this.store_id = '';
        }

        this.searchClick();
      });
    },
    searchClick() {
      this.fetchData();
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      this.list = [];
      if (this.formInline.order_id) {
        this.store_id = this.store_list[0].id;
      }
      if (this.store_id == -1 || this.formInline.order_id) {
        this.formInline.store_id = this.all_store_id;
      } else {
        this.formInline.store_id = this.store_id;
      }
      this.formInline.type = this.type;
      getPage(this.formInline)
        .then(response => {
          this.totalDataNumber = response.message.page * 20;
          getList(this.formInline).then(res => {
            if (res.code == 1) {
              this.list = res.message;
            }
            this.listLoading = false;
          });
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    handleCurrentChange(val) {
      this.listLoading = true;
      this.list = [];
      getList(this.formInline, val).then(response => {
        this.list = response.message;
        this.listLoading = false;
      });
    }
  }
};
</script>